package com.accp.controller;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.accp.common.DataResult;
import com.accp.entity.Comments;
import com.accp.entity.Users;
import com.accp.service.CommentsService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jack
 * @since 2021-08-03
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {

	@Autowired
	CommentsService commentsService;
	
	@PostMapping("/add")
	public DataResult addComment(Comments comments,HttpSession session){
		//如果有异常方法，前端可以接收到吗 -> 通过全局异常处理
		//返回当前评论时间的问题
		//1.返回时间 2.返回整个评论对象(能够获取自增的id) 3.只返回成功标志  4.所有评论直接返回
		Users user = (Users) session.getAttribute("user");
		if(user == null){
			throw new RuntimeException("还没有登录");
		}
		comments.setUid(user.getId());
		comments.setCommentDate(new java.util.Date());
		int result = this.commentsService.addComments(comments);
		return DataResult.getSuccessResult(comments);
	}
	
	@GetMapping("/getByHid")
	public DataResult getCommnetsByHid(int hid){
		List<Comments> comments = this.commentsService.getCommentsByHid(hid);
		return DataResult.getSuccessResult(comments);
	}
}
